package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.pojo.TbAddress;
import com.qf.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Create by 周志豪 on 2022/8/21  14:29
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */

public class AddressDaoImpl implements AddressDao {

    @Override
    public List<TbAddress> findAllAddressByUserId(Long userId) {
        QueryRunner runner = DbUtils.getRunner();
        String sql = "select * from tb_address where uid= ?";
        try {
            List<TbAddress> addressList = runner.query(sql, new BeanListHandler<>(TbAddress.class), userId);
            return addressList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TbAddress findById(Long addressId) {
        String sql = "SELECT * FROM tb_address WHERE id =?";
        QueryRunner runner = DbUtils.getRunner();
        TbAddress address = null;
        try {
            address = runner.query(sql, new BeanHandler<>(TbAddress.class), addressId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return address;
    }
}
